package com.kite;

import org.activiti.engine.*;
import org.activiti.engine.history.HistoricActivityInstance;
import org.activiti.engine.history.HistoricActivityInstanceQuery;
import org.activiti.engine.history.HistoricProcessInstance;
import org.activiti.engine.history.HistoricProcessInstanceQuery;
import org.activiti.engine.impl.util.IoUtil;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentQuery;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.apache.commons.io.IOUtils;
import org.junit.Test;

import java.io.*;
import java.util.List;
import java.util.zip.ZipInputStream;

/**
 * @version V1.0
 * @description:
 * @author: kite
 * @date: 2023/10/25
 */
public class ActivitiDome {

    @Test
    public void test1() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .name("条件部署")
                .addClasspathResource("bpmn/condition.bpmn20.xml")
                .addClasspathResource("bpmn/listener.png")
                .deploy();
        //输出部署信息
        System.out.println("部署id" + deploy.getId());
        System.out.println("部署名称" + deploy.getName());
    }

    @Test
    public void test2() {
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        ProcessInstance evection = runtimeService.startProcessInstanceByKey("evection");

        System.out.println("流程定义id" + evection.getProcessDefinitionId());
        System.out.println("流程实例id" + evection.getId());
        System.out.println("当前活动id" + evection.getActivityId());
    }

    @Test
    public void test3() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();
        taskService.createTaskQuery().processDefinitionKey("evection").taskAssignee("zhangsan").list().forEach(task -> {
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
        });
    }

    @Test
    public void test4() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();
        taskService.complete("7505");
    }

    @Test
    public void test5() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();
        List<Task> evection = taskService.createTaskQuery().processDefinitionKey("evection").list();
        for (Task task : evection) {
            System.out.println("====================================");
            System.out.println("流程实例id：" + task.getProcessInstanceId());
            System.out.println("任务id：" + task.getId());
            System.out.println("任务负责人：" + task.getAssignee());
            System.out.println("任务名称：" + task.getName());
            System.out.println("====================================");
        }
    }

    @Test
    public void test6() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        TaskService taskService = defaultProcessEngine.getTaskService();
        Task task = taskService.createTaskQuery().processDefinitionKey("evection")
                .taskAssignee("zhangsan").singleResult();

        System.out.println("流程实例id：" + task.getProcessInstanceId());
        System.out.println("任务id：" + task.getId());
        System.out.println("任务负责人：" + task.getAssignee());
        System.out.println("任务名称：" + task.getName());
        taskService.complete(task.getId());
    }

    @Test
    public void test7() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        HistoryService historyService = defaultProcessEngine.getHistoryService();
        TaskService taskService = defaultProcessEngine.getTaskService();
        Task task = taskService.createTaskQuery().processDefinitionKey("evection")
                .taskAssignee("lisi").singleResult();


        HistoricActivityInstanceQuery historicActivityInstanceQuery = historyService.createHistoricActivityInstanceQuery();
        historicActivityInstanceQuery.orderByHistoricActivityInstanceStartTime().asc();
        List<HistoricActivityInstance> list = historicActivityInstanceQuery.processInstanceId(task.getProcessInstanceId()).list();

        for (HistoricActivityInstance historicActivityInstanceQuery1 : list) {
            System.out.println("活动id：" + historicActivityInstanceQuery1.getActivityId());
            System.out.println("活动名称：" + historicActivityInstanceQuery1.getActivityName());
            System.out.println("活动类型：" + historicActivityInstanceQuery1.getActivityType());
            System.out.println("====================================");
        }

    }

    @Test
    public void test8() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        InputStream resourceAsStream = this.getClass().getResourceAsStream("/bpmn/listener.zip");
        ZipInputStream zipInputStream = new ZipInputStream(resourceAsStream);

        Deployment deploy = defaultProcessEngine.getRepositoryService().createDeployment()
                .addZipInputStream(zipInputStream)
                .deploy();
        System.out.println("部署id" + deploy.getId());
        System.out.println("部署名称" + deploy.getName());
    }

    @Test
    public void test9() {
        // 流程部署id
        String deploymentId = "1";

        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        // 通过流程引擎获取repositoryService
        RepositoryService repositoryService = processEngine
                .getRepositoryService();
        //删除流程定义，如果该流程定义已有流程实例启动则删除时出错
        repositoryService.deleteDeployment(deploymentId);
    }


    @Test
    public void test10() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        DeploymentQuery deploymentQuery = repositoryService.createDeploymentQuery();
        DeploymentQuery evection = deploymentQuery.processDefinitionKey("evection");
        List<Deployment> list = evection.list();
        for (Deployment deployment : list) {
            System.out.println("部署id" + deployment.getId());
            System.out.println("部署名称" + deployment.getName());
            System.out.println("部署时间" + deployment.getDeploymentTime());
            System.out.println("部署类别" + deployment.getCategory());
            System.out.println("部署人" + deployment.getTenantId());
        }
    }

    @Test
    public void test11() {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        repositoryService.createDeploymentQuery().list().stream().forEach(o->{
            repositoryService.deleteDeployment(o.getId(), true);
        });

    }

    @Test
    public void test12() throws IOException {
        ProcessEngine defaultProcessEngine = ProcessEngines.getDefaultProcessEngine();
        RepositoryService repositoryService = defaultProcessEngine.getRepositoryService();
        ProcessDefinitionQuery processDefinitionQuery = repositoryService.createProcessDefinitionQuery();
        ProcessDefinition evection = processDefinitionQuery.processDefinitionKey("evection").singleResult();

        String deploymentId = evection.getDeploymentId();

        String diagramResourceName = evection.getDiagramResourceName();
        InputStream png = repositoryService.getResourceAsStream(deploymentId, diagramResourceName);

        String resourceName = evection.getResourceName();
        InputStream bpmn = repositoryService.getResourceAsStream(deploymentId, resourceName);

        File pngFile = new File("D:/evection.png");
        FileOutputStream fileOutputStream = new FileOutputStream(pngFile);

        File bpmFile = new File("D:/evection.bpmn");
        FileOutputStream fileOutputStream1 = new FileOutputStream(bpmFile);
        IOUtils.copy(png, fileOutputStream);
        IOUtils.copy(bpmn, fileOutputStream1);

        fileOutputStream.close();


    }
}
